package com.yangyongchao.dailyassistant.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by YangYongchao on 2017-07-22.
 */
public class GetJsonFronHttp{
    public static String getBytesFromHttp(String url){
        HttpURLConnection connection = null;
        URL url1 = null;
        try {
            url1 = new URL(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            connection = (HttpURLConnection)url1.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(5000);
            connection.setReadTimeout(5000);
            InputStream in =connection.getInputStream();
            BufferedReader buff  =new BufferedReader(new InputStreamReader(in));
            //对获取的输入流进行读取
            StringBuffer response = new StringBuffer();
            String line;
            while ((line =buff.readLine())!=null){
                response.append(line);
            }
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (connection!=null){
                connection.disconnect();
            }
        }
        return null;
    }
}
